--配置串口0的行为
local uartid = 0
uart.setup(uartid,921600,8,1)

--串口输出
function port.send(s)
    uart.write(uartid,s)
end

--串口接收函数
--需要在外部代码使用port.recv对接
uart.on(uartid, "receive", function(id, len)
    local s = ""
    repeat
        s = uart.read(id, len)
        if #s > 0 then
            if port.recv then
                port.recv(s)
            end
        end
    until s == ""
end)

print = function(...)
    local data = {...}
    local toSend = {}
    for _,j in pairs(data) do
        table.insert(toSend,tostring(j))
    end
    port.send(table.concat(toSend," "))
    port.send("\n")
    timer.mdelay(200)
end

check_eq = function(a,b,s)
    if a ~= b then
        port.send("not eq!\n")
        port.send(tostring(a))
        port.send("\n")
        port.send(tostring(b))
        port.send("\n")
        port.send(tostring(s) or "")
        port.send("\nline number:")
        port.send(debug.getinfo(2).currentline)
        port.send("\n")
        assert(false)
    end
end

check_nil = function(n,s)
    if not n then
        port.send("found nil/false!\n")
        port.send(tostring(s) or "")
        port.send("\nline number:")
        port.send(debug.getinfo(2).currentline)
        port.send("\n")
        assert(false)
    end
end

